package com.ccs.onlinemusic.controller;


import com.ccs.onlinemusic.model.Music;
import com.ccs.onlinemusic.model.User;
import com.ccs.onlinemusic.server.LoveMusicServer;
import com.ccs.onlinemusic.tools.Constant;
import com.ccs.onlinemusic.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("loveMusic")
@RestController
public class LoveMusicController {

    @Autowired
    private LoveMusicServer loveMusicServer;

    //点赞音乐
    @RequestMapping("likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest req){
        //判断有没有登录

        //没有session不创建
        HttpSession session=req.getSession(false);
        //session和constant为空时 返回没有登录
        if(session==null || Constant.USERINFO_SESSION_KEY==null){
            log.info("没有登录");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        //反之 记录该用户信息
        User user=(User)session.getAttribute(Constant.USERINFO_SESSION_KEY);

        //获取用户id 音乐id
        int musicId=Integer.parseInt(id);
        int userId= user.getId();
        //查询该用户是否点赞过该音乐
        Music music=loveMusicServer.findLoveMusicByMusicIdAndUserId(userId,musicId);
        //如果点赞过
        if(music!=null){
            return new ResponseBodyMessage<>(-1,"该用户点赞过该音乐",false);
        }else{
            //如果没有 -插入音乐
            boolean ret= loveMusicServer.insertLoveMusic(userId,musicId);
            //插入成功与失败
            if(ret){
                return new ResponseBodyMessage<>(0,"点赞音乐成功",true);
            }else {
                return new ResponseBodyMessage<>(-1,"点赞音乐失败",false);
            }
        }
    }

    //1.登录检查
    //2.获取音乐与用户id 通过server查询结果




    @RequestMapping("/findLoveMusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,HttpServletRequest req){
        //判断登录
        HttpSession session=req.getSession(false);
        if(session ==null || Constant.USERINFO_SESSION_KEY==null){
            return  new ResponseBodyMessage<List<Music>>(-1,"没有登录",null);
        }

        User user=(User)session.getAttribute(Constant.USERINFO_SESSION_KEY);

        int userId= user.getId();

        List<Music> musics=new ArrayList<>();
        if(musicName==null){
            loveMusicServer.findLoveMusicByUserId(userId);
        }else{
            loveMusicServer.findLoveMusicByKeyAndUID(musicName,userId);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的收藏的音乐",musics);
    }

    @RequestMapping("/deleteLoveMusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,HttpServletRequest req){
        HttpSession session= req.getSession(false);
        if(session==null || Constant.USERINFO_SESSION_KEY==null){
            log.info("没有登录");
            return new  ResponseBodyMessage<>(-1,"没有登录",false);
        }

        User user=(User)session.getAttribute(Constant.USERINFO_SESSION_KEY);

        int userId=user.getId();
        int musicId=Integer.parseInt(id);

        int ret=loveMusicServer.deleteLoveMusic(userId,musicId);

        if(ret==1){
            log.info("ret:" + ret);
            return new  ResponseBodyMessage<>(0,"删除成功",true);
        }else{
            return new  ResponseBodyMessage<>(-1,"删除失败",false);
        }
    }




}
